package config

import (
	"errors"
	"flow-calc/common"
	"fmt"
)

/**
 *    Description: 连接器相关配置
 *    Date: 2024/3/15
 */

/* 文件配置:
kisType: conn
cname: 测试KisConnector_1
addrs: '0.0.0.0:9988,0.0.0.0:9999,0.0.0.0:9990'
type: redis
key: userid_orderid_option
params:
  args1: value1
  args2: value2
load: null
save:
  - 测试KisFunction_S1
*/

type ConnectorConfig struct {
	KisType    string   `yaml:"kisType"`
	Cname      string   `yaml:"cname"`
	Addrs      string   `yaml:"addrs"`
	EngineType string   `yaml:"type"`
	Key        string   `yaml:"key"` // key存储媒介引擎类型"Mysql" "Redis" "Kafka"等
	Params     FParam   `yaml:"params"`
	Load       []string `yaml:"load"`
	Save       []string `yaml:"save"`
}

func NewConnectorConfig(cName string, addr string, t common.ConnType, key string, param FParam) *ConnectorConfig {
	config := new(ConnectorConfig)
	config.KisType = common.ConnectionType
	config.Cname = cName
	config.Addrs = addr
	config.EngineType = string(t)
	config.Key = key
	config.Params = param
	return config
}

// WithFunc connector 和 Function 进行关系额绑定
func (connectorConfig *ConnectorConfig) WithFunc(fConfig *FuncConfig) error {
	switch common.FunMode(fConfig.FMode) {
	case common.S:
		connectorConfig.Save = append(connectorConfig.Save, fConfig.FName)
	case common.L:
		connectorConfig.Load = append(connectorConfig.Load, fConfig.FName)
	default:
		return errors.New(fmt.Sprintf("连接器和函数类型: %s 绑定不匹配。", fConfig.FMode))
	}
	return nil
}
